//
// Created by v on 19-6-27.
//

#ifndef JKLIBS_CMMQTTMOSQUITTO_H
#define JKLIBS_CMMQTTMOSQUITTO_H

#include "CMMqttBase.h"
#include "utils/cm_common.h"

#ifdef USE_MQTT_MOSQ
#include "mosquitto.h"

// one progress only call once.
void cm_mqtt_init_all();
void cm_mqtt_deinit_all();

class CMMqttMosquitto : public CMMqttBase
{
  public:
    CMMqttMosquitto(const char *host, int port);
    virtual ~CMMqttMosquitto();

    int connect() CM_CPP_OVERRIDE;

    int disconnect() CM_CPP_OVERRIDE;

    int setWill(const char *topic, const char *data) CM_CPP_OVERRIDE;

    int sub(const char *topic, int qos) CM_CPP_OVERRIDE;

    int pub(const char *topic, const char *data, int qos) CM_CPP_OVERRIDE;

    int init() CM_CPP_OVERRIDE;

    int deinit() CM_CPP_OVERRIDE;

    int setUsername(const char *user, const char *pass) CM_CPP_OVERRIDE;

    int loop() CM_CPP_OVERRIDE;
    int loop_block() CM_CPP_OVERRIDE;

    int reconnect() CM_CPP_OVERRIDE;

  private:
    struct mosquitto *m_mosq;
#ifdef MOSQ_NEW
    mosquitto_property *m_willProps;
#endif
    std::string m_bindAddress;
    int         m_mqtt_v5;
};

#else
void cm_mqtt_init_all();
void cm_mqtt_deinit_all();
class CMMqttMosquitto : public CMMqttBase
{
  public:
    CMMqttMosquitto(const char *host, int port);
    virtual ~CMMqttMosquitto(){};

    int connect() { return 0; }
    CM_CPP_OVERRIDE;

    int disconnect() { return 0; }
    CM_CPP_OVERRIDE;

    int setWill(const char *topic, const char *data) { return 0; }
    CM_CPP_OVERRIDE;

    int sub(const char *topic, int qos) { return 0; }
    CM_CPP_OVERRIDE;

    int pub(const char *topic, const char *data) { return 0; }
    CM_CPP_OVERRIDE;

    int init() { return 0; }
    CM_CPP_OVERRIDE;

    int deinit() { return 0; }
    CM_CPP_OVERRIDE;

    int setUsername(const char *user, const char *pass) { return 0; }
    CM_CPP_OVERRIDE;

    int loop() { return 0; }
    CM_CPP_OVERRIDE;
    int loop_block() { return 0; }
    CM_CPP_OVERRIDE;

    int reconnect() { return 0; }
    CM_CPP_OVERRIDE;

  private:
};

#endif

#endif // JKLIBS_CMMQTTMOSQUITTO_H
